const FormatSpaceString = require('../common/FormatSpaceString');
const ObjId = require('../common/ObjId');

const bool_enum = ['true', 'false'];

class Job {
    constructor(obj) {
        this.oid = obj.oid;
        this.name = obj.name;
        this.description = obj.description;
        this.requirements = obj.requirements;
        this.benefits = obj.benefits;
        this.typeId = obj.typeId;
        this.positions = obj.positions;
        this.location = obj.location;
        this.contact = obj.contact;
        this.salary = obj.salary;
        this.isHot = obj.isHot;
        this.isRecommended = obj.isRecommended;
        this.endTime = obj.endTime;
    }

    check() {
        let flag;
        let oid = new ObjId(this, 'oid');
        flag = oid.check();
        if (!flag.success) {
            return flag;
        }
        let name = new FormatSpaceString(this, 'name', false, false, null, null, null, 100);
        flag = name.check();
        if (!flag.success) {
            return flag;
        }
        this.name = name.value;
        let description = new FormatSpaceString(this, 'description', false, false, null, null, null, 50000);
        flag = description.check();
        if (!flag.success) {
            return flag;
        }
        let requirements = new FormatSpaceString(this, 'requirements', false, false, null, null, null, 50000);
        flag = requirements.check();
        if (!flag.success) {
            return flag;
        }
        let benefits = new FormatSpaceString(this, 'benefits', false, false, null, null, null, 50000);
        flag = benefits.check();
        if (!flag.success) {
            return flag;
        }
        let typeId = new ObjId(this, 'typeId');
        flag = typeId.check();
        if (!flag.success) {
            return flag;
        }
        let positions = parseInt(this.positions);
        if (positions == NaN || positions < 0 || positions > 50000) {
            return { success: false, message: "职位数量不能为空且不能超过50000" };
        }
        let location = new FormatSpaceString(this, 'location', false, false, null, null, null, 150);
        flag = location.check();
        if (!flag.success) {
            return flag;
        }
        let contact = new FormatSpaceString(this, 'contact', false, false, null, null, null, 150);
        flag = contact.check();
        if (!flag.success) {
            return flag;
        }
        let salary = new FormatSpaceString(this, 'salary', false, false, null, null, null, 150);
        flag = salary.check();
        if (!flag.success) {
            return flag;
        }
        if (bool_enum.indexOf(this.isHot) === -1) {
            return { success: false, message: "是否热门错误" };
        }
        if (bool_enum.indexOf(this.isRecommended) === -1) {
            return { success: false, message: "是否推荐错误" };
        }
        return { success: true };
    }
}

module.exports = Job;